// Figure 4: Checks for Discontinuity (H1) //

clear all
cap log close

global path ""
use "$path/Main.dta", clear

* Subfigure (a)
local vartitle : variable label same_party_h1 

preserve
lpoly same_party_h1 margin_rel if margin_rel > 0, gen(marpos polpos) ci se(sepos) nograph degree(2)
lpoly same_party_h1 margin_rel if margin_rel < 0, gen(marneg polneg) ci se(seneg) nograph degree(2)

foreach v in pos neg {
gen ul`v' = pol`v' + 1.95*se`v'
gen ll`v' = pol`v' - 1.95*se`v'
}

sort marpos
twoway (rarea ulpos llpos marpos, fcolor(gs14) lwidth(none)) (rarea ulneg llneg marneg, fcolor(gs14) lwidth(none)) ///
(scatter same_party_h1_mv_25 mv_25, msymbol(circle) mlcolor(black) mfcolor(none)) (line polpos marpos, lpattern(solid)  lcolor(black)) (line polneg marneg, lpattern(solid)  lcolor(black)), ///
xline(0, lpattern(dash) lcolor(black)) graphregion(color(white)) legend(off) ytitle("Party Affiliation") xtitle("Margin") xscale(titlegap(2)) xlabel(-1(0.1)1) 
restore

* Subfigure (b)
local vartitle : variable label sex 

preserve
lpoly sex margin_rel if margin_rel > 0, gen(marpos polpos) ci se(sepos) nograph
lpoly sex margin_rel if margin_rel < 0, gen(marneg polneg) ci se(seneg) nograph

foreach v in pos neg {
gen ul`v' = pol`v' + 1.95*se`v'
gen ll`v' = pol`v' - 1.95*se`v'
}

sort marpos
twoway (rarea ulpos llpos marpos, fcolor(gs14) lwidth(none)) (rarea ulneg llneg marneg, fcolor(gs14) lwidth(none)) ///
(scatter sex_mv_25 mv_25, msymbol(circle) mlcolor(black) mfcolor(none)) (line polpos marpos, lpattern(solid)  lcolor(black)) (line polneg marneg, lpattern(solid)  lcolor(black)), ///
xline(0, lpattern(dash) lcolor(black)) graphregion(color(white)) legend(off) ytitle("Gender (Mayor)") xtitle("Margin") xscale(titlegap(2)) xlabel(-1(0.1)1) 
restore
